<?php

class Application_Model_Role {
	protected $id_role;
	protected $nom;
	
	public function setId($id)
	{
		$this->id_role = $id;
	}

	public function setName($name)
	{
		$this->nom = $name;
	}

	public function getId()
	{
		return $this->id_role;
	}

	public function getName()
	{
		return $this->nom;
	}

	public function fromNom($nom)
	{
		$item=new self;
		$item->nom=$nom;

		$tab=new Application_Model_RolesTable();
		$rows = $tab->fetchAll($tab->select()->where('nom = ?',$nom));
		foreach ($rows as $r)
		{$item->id_role=$r['id_role'];
		}
		return $item;
	}

	public function fromId($id)
	{
		$item=new self;
		$item->id_role=$id;

		$tab=new Application_Model_RolesTable();
		$rows = $tab->fetchAll($tab->select()->where('id_role= ?',$id));
		foreach($rows as $r)
		{
			$item->nom=$r['nom'];
		}
		return $item;
	}

	public function getPermissions()
	{
		$perms=array();

		$tab=new Application_Model_RolePermTable();
		$tab2=new Application_Model_PermissionsTable();
		$rows = $tab->fetchAll($tab->select()->where('id_role= ?',$this->id_role));
		foreach($rows as $r)
		{
			$rows2=$tab2->fetchAll($tab2->select()->where('id_perm= ?',$r['id_perm']));
			foreach($rows2 as $r2)
			{
				$perms[]=$r2['nom'];
			}
		}
		return $perms;
	}


}